package com.sgb.kjwl.view.widget.pick;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.LayoutBiddingListQuotationBinding;
import com.sgb.kjwl.model.entity.pick.EnterpriseBiddingDetailEntity5;
import com.sgb.kjwl.model.entity.pick.EnterpriseBiddingJoinEntity5;
import com.sgb.kjwl.view.ui.adapter.pick.QuotationListAdapter5;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 清单列表
 */
public class PickBiddingListQuotationView extends LinearLayout {
    private Context mContext;
    private LayoutBiddingListQuotationBinding binding;
    private QuotationListAdapter5 quotationListAdapter5; //清单
    private EnterpriseBiddingDetailEntity5 detailData;
    private BigDecimal sum;
    private List<EnterpriseBiddingJoinEntity5.DetailsEntity> priceList = new ArrayList<>();

    public PickBiddingListQuotationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.layout_bidding_list_quotation, this, true);
        binding.etPrice.getContent_edt().setTextSize(getResources().getDimension(R.dimen.sp5));
        initView();
    }

    private void initView() {
        binding.biddingListQuotationRv.setLayoutManager(new LinearLayoutManager(mContext));
        if (quotationListAdapter5 == null) {
            quotationListAdapter5 = new QuotationListAdapter5(mContext, null);
        }
        binding.biddingListQuotationRv.setAdapter(quotationListAdapter5);
        binding.biddingListQuotationRv.getLayoutManager().setAutoMeasureEnabled(false);
        quotationListAdapter5.ISumPrice(new QuotationListAdapter5.ISumPrice() {
            @Override
            public void onSum() {
                BigDecimal sums = new BigDecimal("0.00");
                // 商品材料、机械租赁、设备租赁、车辆租赁，这4种是单价*数量，其他类型都是单价之和
                // 2, "劳务分包" 3, "商品/材料" 4, "专业工程" 6, "勘察设计" 7, "建筑设计" 8, "工程监理" 9, "工程造价咨询" 10, "工程总承包"
                // 1, "机械租赁" 5, "设备租赁" 11, "房产租赁" 12, "车辆租赁"

                //14 土地产权转让、15 房产转让、19 物流货运：报价（元），单价之和
                //16 机械转让、17 设备转让、18 车辆转让：单价（元），单价*数量

                if (detailData.getType() == 3 || detailData.getType() == 1 || detailData.getType() == 5 || detailData.getType() == 12
                        || detailData.getType() == 16 || detailData.getType() == 17 || detailData.getType() == 18) {
                    for (int i = 0; i < quotationListAdapter5.getChangeData().size(); i++) {
                        if (!TextUtils.isEmpty(quotationListAdapter5.getChangeData().get(i).getPrice())) {
                            BigDecimal bigDecimal = new BigDecimal(quotationListAdapter5.getChangeData().get(i).getPrice());
                            String size = "1.00";
                            if (!TextUtils.isEmpty(quotationListAdapter5.getChangeData().get(i).getQuantity())) {
                                size = quotationListAdapter5.getChangeData().get(i).getQuantity();
                            }
                            BigDecimal number = new BigDecimal(size);
                            bigDecimal = bigDecimal.multiply(number);
                            sums = sums.add(bigDecimal);
                        }
                    }
                } else {
                    for (int i = 0; i < quotationListAdapter5.getChangeData().size(); i++) {
                        if (!TextUtils.isEmpty(quotationListAdapter5.getChangeData().get(i).getPrice() + "")) {
                            BigDecimal bigDecimal = new BigDecimal(quotationListAdapter5.getChangeData().get(i).getPrice());
                            sums = sums.add(bigDecimal);
                        }
                    }
                }
                subPice();
            }
        });
    }

    /**
     * 数据回显
     *
     * @param detailData
     */
    public void setData(EnterpriseBiddingDetailEntity5 detailData,String categoryModel) {
        this.detailData = detailData;
        if (quotationListAdapter5 != null) {
            quotationListAdapter5.setDatas(detailData.getGoods());
            quotationListAdapter5.setType(detailData.getType() + "");
            quotationListAdapter5.setCategoryModel(categoryModel);
            if (detailData.getType() == 2 || detailData.getType() == 4) {
                binding.biddingHint.setVisibility(VISIBLE);
            }
        }
        subPice();
    }

    /**
     * 设置金额数
     *
     * @param sum
     */
    public void setETText(String sum) {
        binding.etPrice.setContent(sum);
    }

    /**
     * 获取当前数据的条数
     *
     * @return
     */
    private int getItemCount() {
        if (quotationListAdapter5 == null) {
            return 0;
        }
        return quotationListAdapter5.getItemCount();
    }

    /**
     * 控制当前ui布局的显示和隐藏
     */
    public void setView() {
        if (getItemCount() == 0) {
            binding.emptyView.setVisibility(View.VISIBLE);
            binding.rootLayout.setVisibility(GONE);
        } else {
            binding.rootLayout.setVisibility(View.VISIBLE);
            binding.emptyView.setVisibility(View.GONE);
        }
    }

    /**
     * 控制当前ui布局的显示和隐藏
     */
    public void setView(int type) {
        if (getItemCount() == 0) {
            binding.emptyView.setVisibility(type);
            binding.rootLayout.setVisibility(GONE);
        } else {
            binding.rootLayout.setVisibility(type);
            binding.emptyView.setVisibility(View.GONE);
        }
    }

    /**
     * 未填写报价的标红
     */
    public void setViewRed() {
        for (int i = 0; i < quotationListAdapter5.getChangeData().size(); i++) {
            if (TextUtils.isEmpty(quotationListAdapter5.getChangeData().get(i).getPrice()) || "0".equals(quotationListAdapter5.getChangeData().get(i).getPrice()) ) {
                quotationListAdapter5.setShowRed(true);
                return;
            }
        }
    }

    /**
     * 获取报价填写信息
     */
    public BigDecimal subPice() {
        sum = new BigDecimal("0.00");
        priceList.clear();
        for (EnterpriseBiddingDetailEntity5.GoodsEntity goodsEntity : quotationListAdapter5.getDatas()) {
            EnterpriseBiddingJoinEntity5.DetailsEntity detailsEntity = new EnterpriseBiddingJoinEntity5.DetailsEntity();
            detailsEntity.setGoodsId(goodsEntity.getGoodsId());
            detailsEntity.setGoods(goodsEntity.getGoodsId());
            detailsEntity.setNum(goodsEntity.getQuantity());
            detailsEntity.setPrice(goodsEntity.getPrice() + "");
            detailsEntity.setRemark(goodsEntity.getRemark());
            priceList.add(detailsEntity);
        }

        if (priceList != null && priceList.size() != 0) {
            for (int i = 0; i < priceList.size(); i++) {
                if (priceList.get(i).getPrice().equals("0")) {
                    priceList.get(i).setPrice("");
                }
                if (priceList.get(i).getPrice().equals("0.00")) {
                    priceList.get(i).setPrice("");
                }
                if (TextUtils.isEmpty(priceList.get(i).getPrice())) {
                    sum = new BigDecimal("0.00");
                    return sum;
                } else {
                    BigDecimal bigDecimal = new BigDecimal(priceList.get(i).getPrice());
                    if (detailData.getType() == 3 || detailData.getType() == 1 || detailData.getType() == 5 || detailData.getType() == 12
                            || detailData.getType() == 16 || detailData.getType() == 17 || detailData.getType() == 18) {
                        String size = "1.00";
                        if (!TextUtils.isEmpty(priceList.get(i).getNum())) {
                            size = priceList.get(i).getNum();
                        }
                        BigDecimal number = new BigDecimal(size);
                        bigDecimal = bigDecimal.multiply(number).setScale(4, BigDecimal.ROUND_HALF_UP);
                    }
                    sum = sum.add(bigDecimal).setScale(4, BigDecimal.ROUND_HALF_UP);
                }
            }
        }
        setETText(sum.stripTrailingZeros().toPlainString());
        return sum.stripTrailingZeros();
    }

    public List<EnterpriseBiddingJoinEntity5.DetailsEntity> getPriceList() {
        return priceList;
    }


}

